<?php
class SeoController extends Controller {
	public function index() {
		$this->getSmarty ();
        //select p.id,p.`name`,p.`value` from tb_system_param p where p.`name` in ('website_title','website_keywords');
        $paramarray = array("'home_title'","'home_keywords'","'home_desc'",
                             "'category_title'","'category_keywords'","'category_desc'",
                            "'post_title'","'post_keywords'","'post_desc'",
                             "'tag_title'","'tag_keywords'","'tag_desc'"
                            );
        $this->initSEOParam($paramarray);
        $paramstr  = implode(",", $paramarray);
        $sqlParamSql = "select id,name,value from tb_system_param  where name in ($paramstr)";
        require_once SERVICE . DS . 'admin/CommonService.class.php';
        $commonService = new CommonService( $this->getDB() );
        $params = $commonService->query($sqlParamSql);
        $paramkv = array();
        for($i=0;$i<count($params);$i++){
            $paramkv[$params[$i]->name] = $params[$i]->value;
        }

        $this->smarty->assign("params",$paramkv);
		$this->smarty->display ( "admin/seo/seo.tpl" );
	}

    public function initSEOParam($paramarray){
        $paramstr  = implode(",", $paramarray);
        $sqlParamSql = "select count(p.name) from tb_system_param p where p.name in ($paramstr)";
        require_once SERVICE . DS . 'admin/CommonService.class.php';
        $commonService = new CommonService( $this->getDB() );
        $params = $commonService->query($sqlParamSql);
        if(count($params) < 10){
            for($i=0;$i<count($paramarray);$i++){
                $paramName = $paramarray[$i];
               $judeSql = "select id from tb_system_param where name=$paramName";
                $paramid = $commonService->query($judeSql);
                if(count($paramid) == 0){
                    $commonService->insert("tb_system_param",array("name"=>substr($paramName,1,strlen($paramName)-2)));
                }
            }
        }
    }
	public function update() {
		$this->getSmarty ();
        require_once SERVICE . DS . 'admin/CommonService.class.php';
        $commonService = new CommonService( $this->getDB() );
        if (! CommonBase::isPost ()) {
            $this->smarty->display ( "admin/seo/seo.tpl" );
            return;
        }
        $formtype = $title = $keywords = $desc = '';
        extract ( $_POST, EXTR_IF_EXISTS );


        if($formtype == 0){
            $commonService->query("update tb_system_param set value='$title',paramdesc='首页标题' where name='home_title'");
            $commonService->query("update tb_system_param set value='$keywords',paramdesc='首页关键字' where name='home_keywords'");
            $commonService->query("update tb_system_param set value='$desc',paramdesc='首页描述' where name='home_desc'");
        }else if($formtype == 1){
            $commonService->query("update tb_system_param set value='$title',paramdesc='分类页标题' where name='category_title'");
            $commonService->query("update tb_system_param set value='$keywords',paramdesc='分类页关键字' where name='category_keywords'");
            $commonService->query("update tb_system_param set value='$desc',paramdesc='分类页描述' where name='category_desc'");
        }else if($formtype == 2){
            $commonService->query("update tb_system_param set value='$title',paramdesc='文章页标题' where name='post_title'");
            $commonService->query("update tb_system_param set value='$keywords',paramdesc='文章页关键字' where name='post_keywords'");
            $commonService->query("update tb_system_param set value='$desc',paramdesc='文章页描述' where name='post_desc'");
        }else if($formtype == 3){
            $commonService->query("update tb_system_param set value='$title',paramdesc='TAGd页标题' where name='tag_title'");
            $commonService->query("update tb_system_param set value='$keywords',paramdesc='TAGd页关键字' where name='tag_keywords'");
            $commonService->query("update tb_system_param set value='$desc',paramdesc='TAGd页描述' where name='tag_desc'");
        }

        $result = array("success"=>true);


		echo json_encode($result);
	}
	public function delete() {
		$this->getSmarty ();

		echo 1;
	}
	function getpage() {
		$this->getSmarty ();

		echo 2;
		// $this->smarty->display("admin/user_list.tpl");
	}
	public function getStaticSites() {
		$this->getSmarty ();

		echo 1;
	}
	
	
}